package com.link.base.base.quote.controller;

import com.link.base.base.quote.service.QuoteItemService;
import com.link.base.base.quote.model.Quote;
import com.link.base.base.quote.model.QuoteItem;
import com.link.base.base.quote.service.QuoteService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.dao.KeyGenerateDao;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.DateUtil;
import com.link.core.util.LovUtil;
import com.link.core.util.StringUtils;
import com.link.core.cllog.LCLogger;
import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 报价单
 */
@Controller
@RequestMapping("/link/quote")
public class QuoteController extends BasicController<Quote> {
    @Resource
    private QuoteService quoteService;
    @Resource
    private QuoteItemService quoteItemService;
    @Resource
    private KeyGenerateDao keyGenerateDao;
    @Resource
    private JdbcTemplate jdbcTemplate;

    /**
     * 获取Service
     */
    @Override
    public BasicService<Quote> getBasicService() throws Exception {
        if (quoteService != null) {
            return quoteService;
        }
        throw new ServiceException("PUBLIC-005");
    }

    /**
     * <p>后台返回默认值，现是返回报价单编号默认值</p>
     *
     * @author lc
     * @return
     */
    @Override
    @RequestMapping(value = "/preDefaultValue")
    @ResponseBody
    public Map<String, Object> preDefaultValue(Quote entity, HttpSession session, HttpServletRequest request,
                                               HttpServletResponse response) {
        LCLogger.info().withMessageKey("preDefaultValue")
                .withMessage("获取新建默认值...").flush();
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Quote record = new Quote();
            Long rowId = keyGenerateDao.keyGenerate();
            record.setId(rowId);
            String quoteNumb = keyGenerateDao.keyGenerate().toString();
            record.setQuoteNumb(quoteNumb);
            result.put("success", true);
            result.put("result", record);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * 提交报价
     */
    @RequestMapping(value = "/quoteSubmit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> quoteSubmit(@JsonParam Quote record, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            quoteService.quoteSubmit(record);
            result.put("newRow", quoteService.queryById(record));
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 审批通过报价
     */
    @RequestMapping(value = "/quoteApprove", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> quoteApprove(Quote record, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            quoteService.quoteApprove(record);
            result.put("newRow", quoteService.queryById(record));
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 审批拒绝报价
     */
    @RequestMapping(value = "/quoteReject", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> quoteReject(Quote record, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            quoteService.quoteReject(record);
            result.put("newRow", quoteService.queryById(record));
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 取消报价
     */
    @RequestMapping(value = "/quoteCancel", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> quoteCancel(@JsonParam Quote record, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            quoteService.quoteCancel(record);
            result.put("newRow", quoteService.queryById(record));
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * <p>修订报价，即深度复制，先在数据库层复制行，然后返回头id，前端修改头数据后再另行保存，</p>
     * <p>若撤销，复制的行即成为辣鸡数据，无影响</p>
     *
     * @author yrf
     * @param id
     * @return
     */
    @RequestMapping("/quoteEdit")
    @ResponseBody
    public Map<String, Object> quoteEdit(String id) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (StringUtils.isNull(id)) {
                result.put("success", false);
                result.put("result", "报价id不能为空");
                return result;
            }
            Long newId = keyGenerateDao.keyGenerate();

            QuoteItem entity = new QuoteItem();
            entity.setQuoteId(Long.parseLong(id));
            entity.setPageFlag(false);
            List<QuoteItem> list = quoteItemService.queryByExamplePage(entity);
            for (int i = 0; i < list.size(); i++) {
                QuoteItem item = list.get(i);
                item.setId(keyGenerateDao.keyGenerate());
                item.setQuoteId(newId);
                quoteItemService.insert(item);
            }
            result.put("newId", newId);
            String newQuoteNumb = keyGenerateDao.keyGenerate().toString();
            result.put("newQuoteNumb", newQuoteNumb);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 报价转订单
     */
    @RequestMapping(value = "/quoteGenerateOrder", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> quoteGenerateOrder(Quote record, QueryParams qps, HttpSession session,
                                                  HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            record.setQuoteStatus(LovUtil.getLovName("QUOTE_STATUS", "Closed"));
            // 设置转订单时间
            record.setTurnOrderDate(DateUtil.dateToStr(new Date(), "yyyy-MM-dd"));
            QuoteItem entity = new QuoteItem();
            entity.setQuoteId(record.getId());
            entity.setPageFlag(false);
            List<QuoteItem> list = quoteItemService.queryByExamplePage(entity);
            record.setProdList(list);

            quoteService.quoteGenerateOrder(record);
            quoteService.update(record);
            result.put("newRow", quoteService.queryById(record));
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
